package com.nyist.red2;

import java.util.concurrent.CountDownLatch;

public class UserThread2 implements Runnable {
    private final RedEnvelope2 redEnvelope2;
    //用来存放一个闭锁
    private final CountDownLatch latch;

    public UserThread2(RedEnvelope2 redEnvelope2, CountDownLatch latch) {
        this.latch = latch;
        this.redEnvelope2 = redEnvelope2;
    }

    @Override
    public void run() {
        try {
            double money = redEnvelope2.getRandomMoney();
            if (money == 0) {
                System.out.println(Thread.currentThread().getName() + "不好意思，您手慢了！");
            } else {
                System.out.println(Thread.currentThread().getName() + "抢到 " + money + "元");
            }
        } finally {
            latch.countDown();//让CountDownLatch计数器减1
        }
    }
}